#include "Timer.h"



extern uint16_t num;

void Timer_Init(void)
{
	//1.RCC开时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	//2.选择时钟源
	TIM_InternalClockConfig(TIM2);//不调用这个函数也是默认使用内部时钟
	//3.配置时基单元
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	//定时频率=时钟频率/(PSC+1)/(ARR+1)  
	TIM_TimeBaseInitStructure.TIM_Period = 10000-1;//重装载
	TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1;//预分频（越小频率越高）
	//上诉配置定时器周期为1s
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//高级定时器，不用的话给0
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	//4.配置输出中断控制到NVIC
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	//5.配置NVIC
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	//6.使能计数器
	TIM_Cmd(TIM2,ENABLE);
}

void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
	{
		num++;
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
	}
}
